package com.lebang.controller.api;

import com.lebang.component.RedisClient;
import com.lebang.entity.RequestHeaders;
import com.lebang.service.buss.MemberService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Author: xwj
 * @Date: 2020/04/09 16:16
 * @Version 1.0
 */
@Api(tags = "新手任务点击")
@RequestMapping("/api/taskclick")
@RestController
public class TaskClickController {

    @Autowired
    private RedisClient redisClient;

    @Autowired
    private MemberService memberService;

    public static String newHandKey = "newHand:click:";

    @ApiOperation("新手任务点击计数")
    @PostMapping("/count")
    public JsonData count(HttpServletRequest request, String rewardId) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        LocalDate date = LocalDate.now();
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyyMMdd");
        String dateStr = date.format(fmt);
        String memberId = requestHeaders.getUserid();
        String appStore = requestHeaders.getAppStore();
        LocalDateTime createTime = memberService.selectCreateById(memberId);
        LocalDate register = createTime.toLocalDate();

        if (date.isEqual(register)) {
            //同一天,新用户
            //单渠道新用户
            redisClient.hincrBy(newHandKey + dateStr, rewardId + ":" + appStore + ":newUser", 1L);
            //全渠道新用户
            redisClient.hincrBy(newHandKey + dateStr, rewardId + ":allBrand:newUser", 1L);
        } else {
            //不是同一天,老用户
            //单渠道老用户
            redisClient.hincrBy(newHandKey + dateStr, rewardId + ":" + appStore + ":oldUser", 1L);
            //全渠道老用户
            redisClient.hincrBy(newHandKey + dateStr, rewardId + ":allBrand:oldUser", 1L);
        }
        //单渠道全用户
        redisClient.hincrBy(newHandKey + dateStr, rewardId + ":" + appStore + ":allUser", 1L);
        //全渠道全用户
        redisClient.hincrBy(newHandKey + dateStr, rewardId + ":allBrand:allUser", 1L);

        //过期时间修改注释
        //redisClient.expire(newHandKey + dateStr, 3 * 24 * 60 * 60);


        return Callback.success("success", "新增成功");
    }


}

